﻿using ReflectionStudio.Core.Events;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Globalization;
using System.Windows;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    /// <summary>
    /// 专用于坯布检验
    /// 1.扫描布卷号，进入验布，读取生产单信息
    /// 2.输入长度，修改数据库记录
    /// 3.剪出新布卷
    /// 4.打印标签
    /// 5.疵点保存修正
    /// 6.事件记录
    /// 7.纬纱扣除
    /// 8.织轴扣除
    /// </summary>
    public partial class TSYHQI03
    {

        public TSYHQI03()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VWBBST();
            this.DataContext = MV;
            InitializeComponent();
            if (app.company=="KWT")
            {
                this.btnPrint.Content = "完成";
            }           
        }
        public override void QueryCommand(object sender, RoutedEventArgs e)
        {
            if (IsEdit)
            {
                SaveCommand(null, null);
                PM["QIMACHINE"] = MACHINEID;
                TMV.RVZEROWB(PM);
            }
            STKNR = "";
            TBMain.Clear();
            base.QueryCommand(sender, e);
        }
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            if (IsLoaded)
                return;           
            base.Window_Loaded(sender, e);
            this.stopwatch.Start();
        }
        public override void OnCOnlineChg(bool isonline)
        {
            if (!isonline)
                return;
            string key = cRK.KEY;
            cRK.KEY = "";
            cRK.KEY = key;
            base.OnCOnlineChg(isonline);
        }
        public override void CutFCommand(object sender, RoutedEventArgs e)
        {
            try
            { 
                if (cWB08.QTY == 0)
                {
                    throw new Exception("标签长度为0");

                }
                if (TMV.WBBSSTKLG == 0)
                {
                    throw new Exception("请输入落布长度");
                }
                if (LOOMID.Trim().Length == 0)
                {
                    throw new Exception("请补填织机号");
                } 
                if (app.company=="KWT")
                {
                    if (cWB08.QUALITY.Substring(0, 2) == "1C")
                    {
                        if (MessageBox.Show("请与班长确认后再判定!此布卷等级为C级，请确认！", "提示信息", MessageBoxButton.OKCancel, MessageBoxImage.Warning) != MessageBoxResult.OK)
                        {
                            return;
                        }

                    }
                }              
                base.CutFCommand(sender, e);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                Tracer.Error(ex.StackTrace, ex.Message);
            }
        }
        public override void INTEXVALIDATE()
        {
            try
            {
                if (TBMain.Rows.Count > 0)
                    return;
                SResult = QEXTSOURCE(PM);
                if (SResult == null || SResult.Tables[0].Rows.Count == 0)
                {
                    SResult = QEXTSOURCE(PM);
                    if (SResult==null||SResult.Tables[0].Rows.Count == 0)
                    {
                        IsBusy = false;
                        MessageBox.Show("未查询到生产单");
                        return;
                    }
                   
                }
                IsBusy = true;
                WFART = "";
                WAART = "";
                EXORD = "";

                //WBBVAR  WBBFARBE WBBBESCHICH WBBDESS WBBCOLORIT WABEZ
                foreach (DataRow row in SResult.Tables[0].Rows)
                {
                    WFART = "F" + Helper.Left(row["WBBART"].ToString().PadRight(20), 20)
                        + Helper.Left(row["WBBVAR"].ToString().PadRight(2), 2)
                        + Helper.Left(row["WBBFARBE"].ToString().PadRight(10), 10)
                        + Helper.Left(row["WBBBESCHICH"].ToString().PadRight(10), 10)
                         + Helper.Left(row["WBBDESS"].ToString().PadRight(10), 10)
                        + Helper.Left(row["WBBCOLORIT"].ToString().PadRight(10), 10)
                           + "".PadRight(7)
                        + Helper.Left(Helper.ConvertGB2312To8859P1(row["WABEZ"].ToString().PadRight(20)), 20);
                    WAART = WFART.Substring(0, 23);
                    DES = Helper.Left(Helper.ConvertGB2312To8859P1(row["WABEZ"].ToString().PadRight(20)), 20);
                    PARTIE = Helper.Left(Helper.ConvertGB2312To8859P1(row["PARTIE"].ToString().PadRight(20)), 20);
                    EXORD = Helper.Left(row["PRODORDER"].ToString().PadRight(11), 11)
                       + WFART
                        + Helper.Left(row["KDKUNDNA"].ToString().PadRight(8), 8)
                        + Helper.Left(row["LABELTYP"].ToString().PadRight(20), 20)
                        + PARTIE;
                    break;
                }
                base.INTEXVALIDATE();
                IsBusy = false;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public override void OnSTKNRChg(string value)
        {
            if (STKNR == "16301901")
                return;
            if (STKNR == "")
                return;
            Query();
            IsBusy = false;
            base.OnSTKNRChg(value);
        }
        //通过外部数控查询，获取品种，销售单，生产单，织机机台等相关信息
        public DataSet QEXTSOURCE(Dictionary<string, object> pm)
        {
            try
            {
                if (LOOMID == "")
                    throw new Exception("请输入织机号码");
                string chg = "2016-5-14 7L30:00";
                int midx = -1;
                // midx = chg.IndexOf(' '); 
                //chg = chg.Substring(0, midx).Replace('-', '/');
                //MessageBox.Show(chg);
                //string predays = Helper.GetPreviousday(-3);// + "' AND CHANGETIME>'"+ predays.Substring(0,4)+"-"+predays.Substring(4,2)+"-" + predays.Substring(6, 2)
                string sql = " PC.Status = 0   AND MC.MachineName = '" + LOOMID + "'";
                //MessageBox.Show(sql);
                if (app.IsSQLDEMO)
                {
                    SResult = TMV.QWBEXDEMO(sql);
                }
                else
                {
                    SResult = TMV.QWBEXRTJK(sql);
                    // MessageBox.Show(SResult.Tables[0].Rows.Count.ToString());
                    DateTime now = DateTime.Now;
                    bool IsFound = true;
                    int idx = 0;
                    while (true)
                    {
                        midx = -1;
                        idx++;
                        if (idx > 5)
                            break;
                        foreach (DataRow row in SResult.Tables[0].Rows)
                        {
                            IsFound = false;
                            chg = row["CHANGETIME"].ToString();
                            // MessageBox.Show(chg);
                            DateTimeFormatInfo dtFormat = new DateTimeFormatInfo();
                            dtFormat.ShortDatePattern = "yyyy/MM/dd";
                            // MessageBox.Show(chg);
                            midx = -1;
                            midx = chg.IndexOf(' ');
                            chg = chg.Substring(0, midx).Replace('-', '/');
                            //MessageBox.Show(chg.Substring(0, 4) + "/" + chg.Substring(midx + 1, nidx - midx) + "/" + chg.Substring(nidx + 1, 2));
                            //DateTime chgtime = Convert.ToDateTime(chg.Substring(0, 4) + "/" + chg.Substring(midx + 1, nidx - midx - 1) + "/" + chg.Substring(nidx + 1, 2), dtFormat);
                            DateTime chgtime = Convert.ToDateTime(chg, dtFormat);
                            //MessageBox.Show(chgtime.ToString());
                            TimeSpan diff = now.Subtract(chgtime);
                            //MessageBox.Show(diff.Days.ToString());
                            if (diff.Days < 3)
                            {
                                if (MessageBox.Show("织物：" + row["WBBART"].ToString().Trim() + "   织物名称：" + row["WABEZ"].ToString().Trim() + "   批号：" + row["PARTIE"].ToString().Trim(), "请确认品种:", MessageBoxButton.YesNo) != MessageBoxResult.Yes)
                                {
                                    // MessageBox.Show(sql);
                                    sql = " ChangeID<" + row["CHANGEID"].ToString() + "  AND MC.MachineName = '" + LOOMID + "'";
                                    SResult = TMV.QWBEXRTJK(sql);
                                }
                                else
                                {
                                    IsFound = true;
                                    break;
                                }
                            }
                            else
                            {
                                IsFound = true;
                            }
                        }
                        if (IsFound)
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);

            }
            return SResult;

        }

        private void ChgArtCommand(object sender, RoutedEventArgs e)
        {

        }


    }
}
